<?php
/**
 * @desc 区域基础价管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_areaprice extends ctrl_base {
    private static $dirName = 'areaprice';
    
    /**
     * @desc 商品列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('areaprice_main'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$class_big_name = isset($_POST['class_big_name']) ? trim($_POST['class_big_name']) : '';
		$class_small_name = isset($_POST['class_small_name']) ? trim($_POST['class_small_name']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($class_big_name)) {
    		$condition['class_big_name'] = $class_big_name;
    	}
		if(!empty($class_small_name)) {
    		$condition['class_small_name'] = $class_small_name;
    	}
		//订单获取
    	$produce_list = model_areaprice :: get_areaprice_list($nextpage,$pagesize,$condition);
    	$total = array_pop($produce_list);
		//模板赋值
    	self::smarty() -> assign('produce_list', $produce_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('class_big_name', $class_big_name);
		self::smarty() -> assign('class_small_name', $class_small_name);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/areaprice_main.html');    	
    }
	//区域基础价列表
	public static function areaprice_list(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('areaprice_list'));
		$produce_id = isset($_GET['produce_id'])?intval($_GET['produce_id']):0;
		$areaprice = model_areaprice::get_areaprice_by_priduceid($produce_id);
		self::smarty()->assign('areaprice',$areaprice);
		self::smarty()->assign('produce_id',$produce_id);
		self::smarty() -> display(self::$dirName . '/areaprice_list.html');
	}
	//添加区域基础价
	public static function areaprice_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('areaprice_add'));
		$produce_id = isset($_GET['produce_id'])?intval($_GET['produce_id']):0;
		if($_POST){

			$areaprice = self::get_request_data();
			if(model_areaprice::areaprice_exists($areaprice['area_name'], $produce_id)){
				self::ajax_ouput(300, '添加失败,该区域基础价已存在');
			}
			$areaprice['produce_id'] = $produce_id;
			$areaprice['author'] = $_SESSION['admin']['username'];
			$areaprice['editer'] = $_SESSION['admin']['username'];
			$result = model_areaprice::areaprice_add($areaprice);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','areaprice_list','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		self::smarty()->assign('produce_id',$produce_id);
		self::smarty() -> display(self::$dirName . '/areaprice_add.html');
	}
	//修改区域基础价
	public static function areaprice_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$areaprice = model_areaprice::get_areaprice_by_id($id);
		$ctrl_tag = 'areaprice_edit_other';
		if($areaprice['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'areaprice_edit_self';
		}
		self::operation(array($ctrl_tag));
		$produce_id = $areaprice['produce_id'];
    	if($_POST){
			$cur_price = $areaprice['cur_price'];
			$areaprice = array();
			$areaprice = self::get_request_data();
			if($cur_price != $areaprice['cur_price']){
				$areaprice['pre_price'] = $cur_price;
			}
			unset($areaprice['id']);
			if(model_areaprice::areaprice_exists($areaprice['area_name'], $produce_id, $id)){
				self::ajax_ouput(300, '修改失败,该区域基础价已存在');
			}
			$areaprice['editer'] = $_SESSION['admin']['username'];
			$result = model_areaprice::update_areaprice_info(explode(',',$id), $areaprice);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','areaprice_list','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
    	self::smarty() -> assign('areaprice', $areaprice);
    	self::smarty() -> display(self::$dirName . '/areaprice_edit.html');
    }
	//删除区域基础价
	public static function areaprice_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('areaprice_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_areaprice::update_areaprice_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','areaprice_list');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
}
